<?php

declare(strict_types=1);

namespace app\shop\controller\api;

use think\Request;
use sheep\exception\SheepException;
use app\shop\model\Category as CategoryModel;

class Category extends ShopBase
{

    public function list(Request $request)
    {
        $id = $request->param('id', 0);
        $category = CategoryModel::where('parent_id', 0)->normal()->order('weigh', 'desc')->order('id', 'desc');
        if ($id) {
            // 指定 id 分类，否则获取 权重最高的一级分类
            $category = $category->where('id', $id);
        }
        $category = $category->findOrFail();

        $childrenString = $category->getChildrenString($category);
        $categories = CategoryModel::where('id', $category->id)->normal()->with([$childrenString])->find();

        return success('商城分类', $categories);
    }
}
